package com.ymt.bpm.web.rest.platform.config;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.model.PService;
import com.ymt.bpm.service.platform.config.SvcService;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.EncodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/platform/config/service")
public class ServiceResource {

    /**
     * Logger
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * SvcService
     */
    @Autowired
    private SvcService ss;

    @GetMapping("/query")
    public JSONObject query(HttpServletRequest req,
                            @RequestParam(name="appId", required = false)String appId,
                            @RequestParam(name="search", required = false)String search,
                            @RequestParam(name="sort", defaultValue = "CREATE_TIME")String sort,
                            @RequestParam(name="order", defaultValue = "DESC")String order,
                            @RequestParam("offset")int offset,
                            @RequestParam("limit")int limit,
                            @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (search!=null && search.length()>0) {
            search = EncodeUtil.decode(search);
        }
        if (log.isDebugEnabled()) {
            log.debug("query tenantId=" + tenantId);
            log.debug("query appId=" + appId);
            log.debug("query search=" + search);
            log.debug("query sort=" + sort);
            log.debug("query order=" + order);
            log.debug("query offset=" + offset);
            log.debug("query limit=" + limit);
            log.debug("query timezoneOffset=" + timezoneOffset);
        }
        return ss.query(tenantId, appId, search, sort, order, offset, limit, timezoneOffset);
    }

    @GetMapping("/{id}")
    public JSONObject get(HttpServletRequest req,
                      @PathVariable("id") String id,
                      @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("get tenantId=" + tenantId);
            log.debug("get id="+id);
        }
        return ss.get(tenantId, id, lang, timezoneOffset);
    }

    @PostMapping("/save")
    public JSONObject save(HttpServletRequest req,
                       PService service) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("save service.id=" + service.getId());
            log.debug("save tenantId="+tenantId);
            log.debug("save loginName="+loginName);
            log.debug("save displayName="+displayName);
        }
        return ss.save(tenantId, service, loginName, displayName);
    }

    @DeleteMapping("/{id}")
    public JSONObject delete(HttpServletRequest req,
                         @PathVariable("id") String id) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("delete tenantId="+tenantId);
            log.debug("delete id=" + id);
        }
        return ss.delete(tenantId, loginName, displayName, id);
    }

    @PostMapping(value="/checkSvcDesc", produces = "plain/text")
    public String checkSvcDesc(HttpServletRequest req,
                              @RequestParam(value="id", required=false)String id,
                              @RequestParam("svcDesc")String svcDesc) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("checkSvcDesc tenantId=" + tenantId);
            log.debug("checkSvcDesc svcDesc=" + svcDesc);
            log.debug("checkSvcDesc id=" + id);
        }
        return ss.checkAttr(tenantId, "svcDesc", svcDesc, id);
    }

}
